package com.spogher.bsc.panels;

import java.io.Serializable;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.spogher.bsc.BscSession;
import com.spogher.bsc.WicketApplication;
import com.spogher.bsc.data.dao.interfaces.ManagerDao;
import com.spogher.bsc.data.dataobjects.Manager;
import com.spogher.bsc.pages.ManagerPage;

public class SignInPanel extends Panel{

	private static final long serialVersionUID = 1L;

	@SpringBean
	private ManagerDao managerDao;

	public SignInPanel(String id) {
		super(id);
		createComponents();
	}

	private void createComponents() {
		//Sign In Form
		final Form<SignInFormObject> signInForm = new Form<SignInFormObject> ("signInForm", 
				new CompoundPropertyModel<SignInFormObject>(new SignInFormObject()));
		signInForm.setOutputMarkupId(true);
		//If already signed in
		final WebMarkupContainer loggedManagerContainer = new WebMarkupContainer("loggedManagerContainer");
		loggedManagerContainer.setOutputMarkupId(true);
		
		//Adding components
		signInForm.add(new TextField<String>("email").setRequired(true));
		signInForm.add(new PasswordTextField("password").setRequired(true));
		AjaxSubmitLink signInLink = new AjaxSubmitLink("signInLink") {
			

			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				SignInFormObject formObject = (SignInFormObject) form.getModelObject();
				processSignIn(formObject.getEmail(), formObject.getPassword());
				setResponsePage(WicketApplication.get().getHomePage());				
			}
		};
		signInForm.add(signInLink);
		signInForm.add(new BookmarkablePageLink<Void>("createManagerLink", ManagerPage.class));
		signInForm.setVisible(!BscSession.get().isAuthenticated());
		add(signInForm);
		
		loggedManagerContainer.add(new Label("loggedManager", new Model<String>(BscSession.get().getLoggedManager())));
		AjaxLink<Void> signOutLink = new AjaxLink<Void>("signOutLink") {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				BscSession.get().setManager(null);
				setResponsePage(WicketApplication.get().getHomePage());				
			}

		
		};
		loggedManagerContainer.add(signOutLink);
		loggedManagerContainer.setVisible(BscSession.get().isAuthenticated());
		add(loggedManagerContainer);
	}

	private void processSignIn(String email, String password) {
		Manager manager = managerDao.getManager(email, password);
		if (manager != null) {
			BscSession.get().setManager(manager);
		}
	}

	private class SignInFormObject implements Serializable {

		private static final long serialVersionUID = 1L;
		private String email;
		private String password;
		public void setEmail(String email) {
			this.email = email;
		}
		public String getEmail() {
			return email;
		}
		public void setPassword(String password) {
			this.password = password;
		}
		public String getPassword() {
			return password;
		}
			
	}
}


